package com.caiya.service.impl;

import com.caiya.cart.dto.cartinfo.CartAllListDto;
import com.caiya.cart.dto.cartinfo.CartInfoListDto;
import com.caiya.cart.entity.CartInfo;
import com.caiya.cart.vo.cartinfo.CartAllListVo;
import com.caiya.cart.vo.cartinfo.CartInfoAddVo;
import com.caiya.cart.vo.cartinfo.CartInfoListVo;
import com.caiya.cart.vo.cartinfo.CartInfoUpdateVo;
import com.caiya.mapper.CartInfoMapper;
import com.caiya.mapper.CartTypeMapper;
import com.caiya.service.CartInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Administrator on 2018/12/3 0003.
 */
@Service
public class CartInfoServiceImpl implements CartInfoService{

    @Autowired
    private CartInfoMapper cartInfoMapper;
    @Autowired
    private CartTypeMapper cartTypeMapper;

    @Override
    public PageInfo<CartInfoListDto> list(CartInfoListVo cartInfoListVo) {
        PageHelper.startPage(cartInfoListVo.getPageNum(),cartInfoListVo.getPageSize());
        List<CartInfoListDto> list = cartInfoMapper.list(cartInfoListVo);
        for (CartInfoListDto cartInfo:list){
            cartInfo.setCartTypeParentName(cartTypeMapper.selectNameById(cartInfo.getCartTypeParentId()));
            cartInfo.setCartTypeName(cartTypeMapper.selectNameById(cartInfo.getCartTypeId()));
        }
        PageInfo<CartInfoListDto> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public PageInfo<CartAllListDto> allList(CartAllListVo cartAllListVo) {
        PageHelper.startPage(cartAllListVo.getPageNum(),cartAllListVo.getPageSize());
        List<CartAllListDto> list = cartInfoMapper.allList(cartAllListVo);
        PageInfo<CartAllListDto> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public int add(CartInfoAddVo cartInfoAddVo) throws Exception {
        if (cartInfoAddVo.getCartNum()==null || cartInfoAddVo.getCartNum().trim().isEmpty()){
            throw new Exception("请填写车牌号");
        }

        CartInfo cartInfo = new CartInfo();
        cartInfo.setCartNum(cartInfoAddVo.getCartNum());
        cartInfo.setUserId(Long.valueOf(cartInfoAddVo.getUserId()));
        cartInfo.setCartTypeId(Long.valueOf(cartInfoAddVo.getCartTypeId()));
        cartInfo.setCartTypeParentId(Long.valueOf(cartInfoAddVo.getCartTypeParentId()));
        cartInfo.setCartTypeParentName(cartTypeMapper.selectNameById(Long.valueOf(cartInfoAddVo.getCartTypeParentId())));
        cartInfo.setCartTypeName(cartTypeMapper.selectNameById(Long.valueOf(cartInfoAddVo.getCartTypeId())));
        return cartInfoMapper.insert(cartInfo);
    }

    @Override
    public int update(CartInfoUpdateVo cartInfoUpdateVo) throws Exception {
        if (cartInfoUpdateVo.getCartNum()==null || cartInfoUpdateVo.getCartNum().trim().isEmpty()){
            throw new Exception("请填写车牌号");
        }
        if (null!=cartInfoUpdateVo.getCartTypeParentId() && !cartInfoUpdateVo.getCartTypeParentId().equals("")){
            cartInfoUpdateVo.setCartTypeParentName(cartTypeMapper.selectNameById(Long.valueOf(cartInfoUpdateVo.getCartTypeParentId())));
        }
        if (null!=cartInfoUpdateVo.getCartTypeId() && !cartInfoUpdateVo.getCartTypeId().equals("")){
            cartInfoUpdateVo.setCartTypeName(cartTypeMapper.selectNameById(Long.valueOf(cartInfoUpdateVo.getCartTypeId())));
        }
        return cartInfoMapper.update(cartInfoUpdateVo);
    }
}
